var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();
var daylineChart = null;
var yuelineChart = null;

Page({

data: {
  sumGradeData:[],
  years:[],
  math:'',
  english:'',
  politics:'',
  professionalCourse:''
},

// -----------------数据加载模块 start---------------
onLoad: function (e) {
  this.getNationalLine();
},
// -----------------数据加载模块 end---------------


// -----------------功能模块 start---------------
// 获取国家线数据
getNationalLine:function(){
  var mark = 'getNationalLine';
  wx.request({
    url: 'http://localhost:8080/youYanZaiXian/nationalLineServlet',
    method: 'POST',
    data:{
      mark:mark,
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    },
    success: (res)=>{
      var nlList = res.data.data.nlList;
      this.setData({
        nationalLineList:nlList,
      })
      //console.log(this.data.nationalLineList)
      this.processDate();  // 由于异步的原因，所以在这里调用数据处理函数,否则得不到数据
    }
  });
  
},

// 将数据库中的数据赋值到data中
processDate:function(){
  var nlList = this.data.nationalLineList;
  var years=[];
  var sumGradeData = [];
  for(var i = 0;i<nlList.length;i++){
    if(i==0){
      this.setData({
        math:nlList[i].math,
        english:nlList[i].english,
        politics:nlList[i].politics,
        professionalCourse:nlList[i].professionalCourse
      })
    }else{
      for(var j=0;j<nlList.length;j++){
        var tempSum = 0;
        tempSum = tempSum+nlList[j].math;
        tempSum = tempSum+nlList[j].english;
        tempSum = tempSum+nlList[j].politics;
        tempSum = tempSum+nlList[j].professionalCourse;
        sumGradeData[j] = tempSum;
      }
      years[i] = nlList[i].year;
    }
  }
  sumGradeData[0] = this.data.math+this.data.english+this.data.politics+this.data.professionalCourse;
  years[0] = nlList[0].year;
  //console.log(sumGradeData);
  //console.log(years);
  this.setData({
    sumGradeData:sumGradeData,
    years:years,
  })
  // 由于异步的原因，所以在这里调用以下两个函数，进行画图，否则得不到数据
  this.sumGrade();
  this.GradeProportion();
},

  // 国家线总分
sumGrade:function(){
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  yuelineChart = new wxCharts({ //当月用电折线图配置
    canvasId: 'sumGrade',
    type: 'line',
    categories: this.data.years, //categories X轴
    animation: true,
    // background: '#f5f5f5',
    series: [{
      name: '国家线',
      data: this.data.sumGradeData,
      format: function (val, name) {
        return val.toFixed(2) + 'kWh';
      }
    }, 
    
    
  ],
    xAxis: {
      disableGrid: true
    },
    yAxis: {
      title: '历年录取总分数',
      format: function (val) {
        return val.toFixed(2);
      },
      max: 20,
      min: 0
    },
    width: windowWidth,
    height: 200,
    dataLabel: false,
    dataPointShape: true,
    extra: {
      lineStyle: 'curve'
    }
  });
},

// 最新分数占比
GradeProportion:function(){
  var windowWidth = 320;
  try {
    var res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  } catch (e) {
    console.error('getSystemInfoSync failed!');
  }
  yuelineChart = new wxCharts({ //当月用电折线图配置
    animation: true, //是否有动画
    canvasId: 'GradeProportion',
    type: 'pie',
    series: [{
      name: '数学',
      data: this.data.math,
  }, {
      name: '专业课',
      data: this.data.professionalCourse,
  }, {
      name: '英语',
      data: this.data.english,
  }, {
    name: '政治',
    data: this.data.politics,
  }],
    width: windowWidth,
    height: 300,
    dataLabel: true,
 });
},
// -----------------功能模块 end---------------

});


